package com.kla.power.controller;

import com.kla.power.entity.InverterElectricEntity;
import com.kla.power.util.AlarmUtil;
import com.kla.power.util.CollectorUtil;
import com.kla.power.util.CommonUtil;
import org.apache.commons.lang.StringUtils;

import java.nio.charset.StandardCharsets;

public class Test {


    /**
     * 将UTF-8编码的字符串转换为16进制字符串表示
     *
     * @param utf8Str UTF-8编码的字符串
     * @return 16进制表示的字符串
     */
    public static String utf8ToHex ( String utf8Str ) {
        if ( utf8Str == null ) {
            return null;
        }
        // 将字符串转换为字节数组
        byte[] bytes = utf8Str.getBytes ( StandardCharsets.UTF_8 );
        // 创建一个StringBuilder来构建16进制字符串
        StringBuilder hexStrBuilder = new StringBuilder ( bytes.length * 2 );
        // 遍历字节数组，将每个字节转换为16进制字符串并添加到StringBuilder中
        for ( byte b : bytes ) {
            hexStrBuilder.append ( String.format ( "%02X", b ) );
        }
        return hexStrBuilder.toString ();
    }


    /**
     * 将字符串转换为16进制表示，但不保证结果长度为10位
     * 注意：此方法可能导致信息丢失，仅适用于短字符串或特定场景
     *
     * @param inputStr 待转换的字符串
     * @return 转换后的16进制字符串
     */
    public static String stringToHex ( String inputStr ) {
        if ( inputStr == null ) {
            return null;
        }

        byte[] bytes = inputStr.getBytes ( StandardCharsets.UTF_8 );
        StringBuilder hexStrBuilder = new StringBuilder ();

        for ( byte b : bytes ) {
            hexStrBuilder.append ( String.format ( "%02X", b ) );
        }

        return hexStrBuilder.toString ();
    }


    public static void main ( String[] args ) {

        String s = CollectorUtil.hexToUtf8 ( "53 34 32 36 30 30 30 30 33 31 37 32 31 33 35 33  ", 0, 0 );
        System.out.println ( s );

        String data = "02 3b 0b ea 0b f2 00 46 00 44 00 af 08 f8 13 85 0f 8c 00 06 dd fe 00 00 00 00 00 01 00 00 00 00 03 5e 01 ae 01 b0 02 33 0e c4 48 b6 31 30 30 36 30 30 30 30 30 31 35 31 30 31 33 36 48 4e 53 36 30 30 30 54 4c 00 00 00 41 d4 4a 55 81 20 20 20 20 20 20 20 20 20 20 53 34 32 36 30 30 30 30 33 31 37 32 31 33 31 39 30 30 30 56 56 31 2d 30 30 30 2d 30 30 30 30 30 31 10 4b 4c 32 30 32 34 30 36 30 37 30 30 30 38 ";

        String data1 = "01 de 00 00 08 4a 00 00 00 6a 00 4b 09 d6 13 8a 07 55 00 05 42 10 00 00 00 00 00 01 00 00 00 00 01 a8 00 00 01 a8 01 b6 10 0c 49 ba 31 30 30 36 30 30 30 30 30 31 35 31 30 31 33 36 48 4e 53 36 30 30 30 54 4c 00 00 00 41 46 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 53 34 32 36 30 30 30 30 33 31 37 32 31 33 35 33 30 30 30 56 56 31 2d 30 30 30 2d 30 30 30 30 30 31 4b 4c 32 30 32 34 30 36 30 37 30 30 30 35";

        parseFrame ( data );
    }

    private static void parseFrame ( String hex ) {
        InverterElectricEntity inverterElectricEntity = new InverterElectricEntity ();
        String data = hex.replace ( " ", "" ).toUpperCase ();
        if ( data.length () / 2 == 135 ) {
            //1.1 解析逆变器数据，前121个数据
            long temp = CollectorUtil.hexToDecimal ( data, 0, 2 );
            inverterElectricEntity.setInverTemp ( CommonUtil.parseDecimalPoint ( 1.0 * temp / 10 ) );
//            long pv1Voltage = CollectorUtil.hexToDecimal ( data, 2, 2 );
//            inverterElectricEntity.setPv1Voltage ( CommonUtil.parseDecimalPoint ( 1.0 * pv1Voltage / 10 ) );
//            long pv2Voltage = CollectorUtil.hexToDecimal ( data, 4, 2 );
//            inverterElectricEntity.setPv2Voltage ( CommonUtil.parseDecimalPoint ( 1.0 * pv2Voltage / 10 ) );
//            long pv1Current = CollectorUtil.hexToDecimal ( data, 6, 2 );
//            inverterElectricEntity.setPv1Current ( CommonUtil.parseDecimalPoint ( 1.0 * pv1Current / 10 ) );
//            long pv2Current = CollectorUtil.hexToDecimal ( data, 8, 2 );
//            inverterElectricEntity.setPv2Current ( CommonUtil.parseDecimalPoint ( 1.0 * pv2Current / 10 ) );
//            long gridCurrent = CollectorUtil.hexToDecimal ( data, 10, 2 );
//            inverterElectricEntity.setGridCurrent ( CommonUtil.parseDecimalPoint ( 1.0 * gridCurrent / 10 ) );
//            long gridVoltage = CollectorUtil.hexToDecimal ( data, 12, 2 );
//            inverterElectricEntity.setGridVoltage ( CommonUtil.parseDecimalPoint ( 1.0 * gridVoltage / 100 ) );
//            long gridFrequency = CollectorUtil.hexToDecimal ( data, 14, 2 );
//            inverterElectricEntity.setGridFrequency ( CommonUtil.parseDecimalPoint ( 1.0 * gridFrequency / 10 ) );
            long gridPower = CollectorUtil.hexToDecimal ( data, 16, 2 );
            inverterElectricEntity.setGridPower ( CommonUtil.parseDecimalPoint ( gridPower ) );
            long totalEnergy = CollectorUtil.hexToDecimal ( data, 18, 4 );
            inverterElectricEntity.setTotaEnergy ( CommonUtil.parseDecimalPoint ( 1.0 * totalEnergy / 10 ) );
            long totalHours = CollectorUtil.hexToDecimal ( data, 22, 4 );
            inverterElectricEntity.setTotalHours ( CommonUtil.parseDecimalPoint ( totalHours ) );
            long model = CollectorUtil.hexToDecimal ( data, 26, 2 );
            inverterElectricEntity.setOperModel ( Integer.parseInt ( String.valueOf ( model ) ) );
            long alarm = CollectorUtil.hexToDecimal ( data, 28, 4 );
            System.out.println ( alarm );

            long today = CollectorUtil.hexToDecimal ( data, 32, 2 );
            inverterElectricEntity.setTodayEnergy ( CommonUtil.parseDecimalPoint ( 1.0 * today / 100 ) );
            long a = CollectorUtil.hexToDecimal ( data, 34, 2 );
            inverterElectricEntity.setChannelAEnergy ( CommonUtil.parseDecimalPoint ( 1.0 * a / 100 ) );
            long b = CollectorUtil.hexToDecimal ( data, 36, 2 );
            inverterElectricEntity.setChannelBEnergy ( CommonUtil.parseDecimalPoint ( 1.0 * b / 100 ) );
            long sink = CollectorUtil.hexToDecimal ( data, 38, 2 );
            inverterElectricEntity.setSinkTemp ( CommonUtil.parseDecimalPoint ( 1.0 * sink / 10 ) );
            long bus = CollectorUtil.hexToDecimal ( data, 40, 2 );
            inverterElectricEntity.setBusVoltage ( CommonUtil.parseDecimalPoint ( 1.0 * bus / 10 ) );
            long pwrOnCnt = CollectorUtil.hexToDecimal ( data, 42, 2 );
            inverterElectricEntity.setPwrOnCnt ( Integer.parseInt ( String.valueOf ( pwrOnCnt ) ) );
            //解析逆变器身份数据，全部都是UTF8格式
            String phaseNumber = CollectorUtil.hexToUtf8 ( data, 44, 1 );//相序
            inverterElectricEntity.setPhaseNumber ( phaseNumber );
            String vaRating = CollectorUtil.hexToUtf8 ( data, 45, 6 );
            inverterElectricEntity.setVaRating ( vaRating );
            //固件序列号
            String firmwareVer = CollectorUtil.hexToUtf8 ( data, 51, 9 );
            inverterElectricEntity.setFirmwareVer ( firmwareVer );
            //型号名称
            String madelName = CollectorUtil.hexToUtf8 ( data, 60, 12 );
            inverterElectricEntity.setMadelName ( madelName );
            //制造商
            String manufacturer = CollectorUtil.hexToUtf8 ( data, 72, 16 );
            inverterElectricEntity.setManufacturer ( manufacturer );
            //SN
            String inverterSn = CollectorUtil.hexToUtf8 ( data, 88, 16 );
            inverterElectricEntity.setSerialNumber ( inverterSn );
            //标称值
            String nomVpv = CollectorUtil.hexToUtf8 ( data, 104, 4 );
            inverterElectricEntity.setNomVpv ( nomVpv );
            String interVersion = CollectorUtil.hexToUtf8 ( data, 108, 13 );

            String alarms = CollectorUtil.parseData ( data, 28, 4 );
            parseAlarm ( alarms, inverterSn );


            inverterElectricEntity.setInterVersion ( interVersion );
            //2.1 解析数采SN，后14个字节
            String snData = CollectorUtil.parseData ( data, 121, 135 );
            String sn = CollectorUtil.hexToUtf8 ( snData, 0, snData.length () / 2 );
            inverterElectricEntity.setCollectorSn ( sn );
        }
        System.out.println ( inverterElectricEntity );
    }

    //0000 0100 1101 1110 1000 1100 1000 0000
    private static void parseAlarm ( String alarm, String inverterSn ) {
        System.out.println ( alarm );
        String s = CommonUtil.decimalToBinary ( Integer.parseInt ( String.valueOf ( alarm ) ) );
        System.out.println ( s );
        s = new StringBuilder ( s ).reverse ().toString ();
        System.out.println ( s );
        StringBuilder stringBuilder = new StringBuilder ();
        for ( int i = 0; i < s.length (); i++ ) {
            if ( s.charAt ( i ) == '1' ) {
                if ( StringUtils.isNotBlank ( AlarmUtil.getAlarm ( i ) ) ) {
                    System.out.print ( i + "   " );
                    System.out.println ( AlarmUtil.getAlarm ( i ) );
                }
            }
        }
        System.out.println ( stringBuilder.toString () );
    }


    //01       01       01       01       01       01       01       01       01       01
    //00000001 00000001 00000001 00000001 00000001 00000001 00000001 00000001 00000001 00000001

    public static String convertToHex ( String utf8String ) {
        byte[] bytes = utf8String.getBytes ( java.nio.charset.StandardCharsets.UTF_8 );
        StringBuilder hexString = new StringBuilder ();
        for ( byte b : bytes ) {
            String hex = Integer.toHexString ( b & 0xFF );
            if ( hex.length () == 1 ) {
                // 如果是一位的话，要补0
                hexString.append ( '0' );
            }
            hexString.append ( hex );
        }
        return hexString.toString ();
    }

}
